﻿/*
版权所有：版权所有(C) 2011，刘建新
文件编号：M01_OrderApplyBll.cs
文件名称：OrderApplyBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2011-01-05
作　　者：
内容摘要：表[Order_Apply]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.Bus;
using System.Data;
using Eis51.CampanyCMS.Model.Bus.QueryO;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.Access.Bus;
using System.Collections;

namespace Eis51.CampanyCMS.Business.Bus
{
    /// <summary>
	/// OrderApply的BLL 自定义方法
	/// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
	/// </summary>
    public partial class OrderApplyBLL : BaseBLL<OrderApply>
    {
        /// <summary>
        /// 查询seller的预定订单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataTable SearchSellerOrderList(OrderApplyQuery query)
        {
            string sql = "select * from Order_Apply where 1=1 ";


            //订单号
            if (!string.IsNullOrEmpty(query.OrderNo))
            {
                sql += " and  Order_NO like '%{0}%' ";
                sql = string.Format(sql, query.OrderNo);
            }
            //预定时间区间
            if (!string.IsNullOrEmpty(query.BeginDate))
            {
                sql += " and  Create_Date >= #{0} 00:00:00# ";
                sql = string.Format(sql, query.BeginDate);
            }
            //预定时间区间
            if (!string.IsNullOrEmpty(query.EndDate))
            {
                sql += " and  Create_Date <= #{0} 23:59:59# ";
                sql = string.Format(sql, query.EndDate);
            }

            if (!string.IsNullOrEmpty(query.OrderStatus))
            {
                sql += " and Order_Status = {0} ";
                sql = string.Format(sql, query.OrderStatus);
            }

            if (!string.IsNullOrEmpty(query.Seller))
            {
                sql += " and   Product_Seller = '{0}' ";
                sql = string.Format(sql, query.Seller);
            }

            sql += " order by Create_Date desc ";
            return this.GetDataTable(sql);
        }


        /// <summary>
        /// 没有
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="list"></param>
        public void Submit(OrderApply obj, List<ShoppingCarEntriy> list)
        {
            Submit(obj);

            List<OrderApplyItem> listItem = new List<OrderApplyItem>();

            foreach (ShoppingCarEntriy tmp in list)
            {
                OrderApplyItem addnew = new OrderApplyItem();
                addnew.ProductId = int.Parse (tmp.ProductId);
                addnew.OrderApplyId = obj.OrderApplyId;
                addnew.LasteUpdateDate = DateTime.Now;
                addnew.CreateDate = DateTime.Now;
                addnew.Count = list.Count;
                addnew.Tatol = Convert.ToDouble(tmp.Total);
                addnew.Vaild = 1;
                addnew.ProductName = tmp.Name;
                addnew.Count = tmp.Count;
                addnew.UnitPrice = Convert.ToDouble(tmp.Price);
                listItem.Add(addnew);
            }
            OrderApplyItemBLL.GetInstance().Submit(listItem, obj.OrderApplyId.ToString());
        }

        public void Delete(string OrderApplyID)
        {
            string sql = string.Format("delete from Order_Apply where Order_Apply_ID ={0} ", OrderApplyID);
            this.Execute(sql);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="obj"></param>
        public void Submit(OrderApply obj)
        {
            obj.LastUpdateDate = DateTime.Now;
            if (obj.OrderApplyId > 0)
            {

                this.Update(obj);
            }
            else
            {
                obj.CreateDate = DateTime.Now;
                this.Insert(obj);
                obj.OrderNo = DateTime.Now.ToString("yyyyMMdd") + obj.OrderApplyId.ToString().PadLeft(10, '0');
                this.Update(obj);
            }
        }

       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userNo"></param>
        /// <returns></returns>
        public DataTable SearchMyOrder(string userNo)
        {
            string sql = string.Format("select * from Order_Apply where res_user_no='{0}' order by order_apply_id desc", userNo);

            return this.GetDataTable(sql);

        }

        public string GetCount(OrderApplyQuery query)
        {
            DataTable dt = SearchBuyOrderList(query);
            if (dt != null && dt.Rows.Count > 0)
            {

                return dt.Rows.Count.ToString();
            }

            return "0";
        }
 
        /// <summary>
        /// 查询seller的预定订单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataTable SearchBuyOrderList(OrderApplyQuery query)
        {
            string sql = "select * from Order_Apply where 1=1 ";


            //订单号
            if (!string.IsNullOrEmpty(query.OrderNo))
            {
                sql += " and  Create_Date like '%{0}%' ";
                sql = string.Format(sql, query.OrderNo);
            }
            //预定时间区间
            if (!string.IsNullOrEmpty(query.BeginDate))
            {
                sql += " and  Create_Date >= #{0} 00:00:00# ";
                sql = string.Format(sql, query.BeginDate);
            }
            //预定时间区间
            if (!string.IsNullOrEmpty(query.EndDate))
            {
                sql += " and  Create_Date <= #{0} 23:59:59# ";
                sql = string.Format(sql, query.EndDate);
            }

            if (!string.IsNullOrEmpty(query.OrderStatus))
            {
                sql += " and Order_Status = {0} ";
                sql = string.Format(sql, query.OrderStatus);
            }

            if (!string.IsNullOrEmpty(query.Res_user_no))
            {
                sql += " and   Res_User_NO = '{0}' ";
                sql = string.Format(sql, query.Res_user_no);
            }

            sql += " order by Create_Date desc ";
            return this.GetDataTable(sql);
        }

        

        public string StatusEn(string v)
        {
            switch (v)
            {
                case "1":
                    return "等待付款";
                case "2":
                    return "已付款";
                case "3":
                    return "已发货";
                case "4":
                    return "完成";
                default:
                    return "";
            }
        }

        public string StatusCn(string v)
        {
            switch (v)
            {
                case "1":
                    return "等待付款";
                case "2":
                    return "已付款";
                case "3":
                    return "已发货";
                case "4":
                    return "完成";
                default:
                    return "";
            }
        }
        public DataTable GetOrderinfo(int Order_Apply_ID)
        {
            string sql = "select * from  Order_Apply as a  where a.Order_Apply_ID={0}  ";
            sql = string.Format(sql, Order_Apply_ID);
            DataTable dt = this.GetDataTable(sql);
            return dt;
        }

        public string GetUsernoByOrderid(string Orderid)
        {
            string Buyid = "";
            OrderApply orderapply = OrderApplyBLL.GetInstance().Get(new   OrderApply.Key(int.Parse(Orderid)));
            Buyid = orderapply.ResUserNo;
            return Buyid;
        }

        public string GetSellerByOrderid(string Orderid)
        {
            string Seller = "";
            OrderApply orderapply = OrderApplyBLL.GetInstance().Get(new  OrderApply.Key(int.Parse(Orderid)));
            Seller = orderapply.ProductSeller;
            return Seller;
        }

        /// <summary>
        /// 我的订单统计查询
        /// </summary>
        /// <returns></returns>
        public DataTable MyOrderStat()
        {
            string sql = @"select 
                    sum(case when order_status =1 then 1 else 0 end) as Moduel1,
                    sum(case when order_status =2 then 1 else 0 end) as Moduel2,
                    sum(case when order_status =3 then 1 else 0 end) as Moduel3,
                    sum(case when order_status =4 then 1 else 0 end) as Moduel4
                     from dbo.Order_Apply where Product_Seller='{0}'";

            sql = string.Format(sql, AppContext.CurrentAdministrator.UserName);
            return this.GetDataTable(sql);
        }

        public bool IsBuy(string productid ,string Username)
        {
            string sql = @"select  1  from   Order_Apply as a inner join Order_Apply_Item as b on a.Order_Apply_ID=b.Order_Apply_ID   where b.Product_ID={0} and a.Order_Status>1  and  a.Res_User_NO='{1}' ";
            sql = string.Format(sql, productid, Username);
            DataTable dt =this.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

    }

    #region 代码自动生成
    /// <summary>
    /// OrderApply的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class OrderApplyBLL : BaseBLL<OrderApply>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: OrderApplyBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected OrderApplyBLL()
        {
        }
        private static volatile OrderApplyBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 OrderApplyBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static OrderApplyBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(OrderApplyBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new OrderApplyBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>OrderApply</returns>
        public OrderApply Get(OrderApply.Key key)
        {

            return OrderApplyDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return OrderApplyDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(OrderApply obj)
        {
            int result = 0;
            try
            {
                result = OrderApplyDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(OrderApply obj)
        {
            OrderApplyDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(OrderApply obj)
        {
            bool result = OrderApplyDao.GetInstance().DeleteByID(new OrderApply.Key(obj.OrderApplyId));
            return result ? 1 : 0;
        }

        #endregion

    }
    #endregion
}